home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Converters / aa_m68k_Intel_HP_Only / ToMPEG.0.8 / Source / ToMPEG.m < prev    next >
Encoding:
Text File  |  1994-11-14  |  10.4 KB  |  435 lines

  1.  
  2. #import "ToMPEG.h"
  3. #include <stdio.h>
  4. #import <stdlib.h>
  5. #import <string.h>
  6. #import <ctype.h>
  7.      #include <sys/file.h>  /* POSIX applications #include <unistd.h> */
  8.  
  9.      #define R_OK    4/* test for read permission */
  10.      #define W_OK    2/* test for write permission */
  11.      #define X_OK    1/* test for execute (search) permission */
  12.      #define F_OK    0/* test for presence of file */
  13.  
  14.      int access(const char *path, int mode);
  15.  
  16. @implementation ToMPEG
  17.  
  18. #define    ANIMDIRECTORY        "~/Library/Images/Movies/Globe.anim"
  19. #define STARTFRAME        1
  20. #define ENDFRAME        72
  21. #define PBMPLUSDIRECTORY     "/usr/local/pbmplus/bin"
  22. #define FRAMEWIDTHxHEIGHT     "640x480"
  23. #define SPECIALFRAMETYPE    "MyType"
  24. /*  The following are the tags set in InterfaceBuilder for the "formatButton" PopUp */
  25. #define TIFF        0
  26. #define GIF        1
  27. #define    GOULD        2
  28. #define    ILBM        3
  29. #define    IMG        4
  30. #define    MTV        5
  31. #define    PCX        6
  32. #define    PGM        7
  33. #define    PI1        8
  34. #define    PICT        9
  35. #define    PJ        10
  36. #define    QRT        11
  37. #define    RAW        12
  38. #define    RGB3        13
  39. #define    SLD        14
  40. #define    SPC        15
  41. #define    SPU        16
  42. #define    TGA        17
  43. #define    XIM        18
  44. #define    XPM        19
  45. #define    YUV        20
  46. #define PPM        21
  47. #define SPECIAL        22
  48.  
  49. char MPEGer[MAXPATHLEN + 1] = "mpeg-encode";
  50. char PPMer[MAXPATHLEN + 1]  = "tiff2ppm";
  51. char converter[MAXPATHLEN + 1] = "giftoppm";
  52. char MyConverter[MAXPATHLEN + 1]  = "MyType2ppm";
  53.  
  54. /*  extract the name of the animation from the full pathname */
  55. char *stripname(char *animdirectory)
  56. {
  57.     char *tmp, *tmp2, *tmp3;
  58.     tmp = (char *)malloc(100);
  59.     strcpy(tmp, animdirectory);
  60.     if((tmp2 = strrchr(tmp, '.'))) (*tmp2) = '\0';
  61.     if((tmp3 = strrchr(tmp, '/'))) return (tmp3 + 1);
  62.     else return tmp;
  63. }
  64.  
  65. /*  This method gets called automatically, since toMPEG is App's delegate    */
  66. char *param[19];
  67. - appDidInit:sender
  68. {
  69.     NXBundle *bundle;
  70.     int i;
  71.     
  72. /*  Initialize pathnames to appwrapper resources: mpeg-encode and tiff2ppm    */
  73.     bundle = [NXBundle bundleForClass:[self class]];
  74.     [bundle getPath:MPEGer forResource:"mpeg-encode" ofType:NULL];
  75.     [bundle getPath:PPMer forResource:"tiff2ppm" ofType:NULL];
  76.  
  77.  
  78. /*  Initialize the param strings                        */
  79.     for(i=0; i<19; i++) param[i] = (char *)malloc(125);
  80.     strcpy(param[0], "PATTERN        IBBPBBPBBPBBPBB");
  81.     strcpy(param[1], "OUTPUT        Globe.mpg");
  82.     strcpy(param[2], "GOP_SIZE    40");
  83.     strcpy(param[3], "SLICES_PER_FRAME    1");
  84.     strcpy(param[4], "BASE_FILE_FORMAT    PPM");
  85.     sprintf(param[5], "INPUT_CONVERT      %s *", PPMer);
  86.     strcpy(param[6], "INPUT_DIR    ~/Library/Images/Movies/Globe.anim");
  87.     strcpy(param[7], "INPUT");
  88.     strcpy(param[8], "Globe.*.tiff    [1-72]");
  89.     strcpy(param[9], "END_INPUT");
  90.     strcpy(param[10], "PIXEL        HALF");
  91.     strcpy(param[11], "RANGE        10");
  92.     strcpy(param[12], "PSEARCH_ALG    LOGARITHMIC");
  93.     strcpy(param[13], "BSEARCH_ALG    CROSS2");
  94.     strcpy(param[14], "IQSCALE        8");
  95.     strcpy(param[15], "PQSCALE        10");
  96.     strcpy(param[16], "BQSCALE        25");
  97.     strcpy(param[17], "REFERENCE_FRAME    ORIGINAL");
  98.     strcpy(param[18], "FORCE_ENCODE_LAST_FRAME");
  99.  
  100. /*  Initialize outlets to the List and the Matrix of the "formatButton" PopUp    */
  101.     if (![formatButton isKindOf:[PopUpList class]]) 
  102.     {
  103.         formatList = [formatButton target];
  104.         formatMatrix = [formatList itemList];
  105.     }
  106.  
  107.     [converterinfo setEnabled:NO];
  108.  
  109.     return self;
  110. }
  111.  
  112. - setDefaults:sender
  113. {
  114. /* initialize animinfo form with default values  */
  115.     [animinfo setStringValue:ANIMDIRECTORY at:0];
  116.     [animinfo setIntValue:STARTFRAME at:1];
  117.     [animinfo setIntValue:ENDFRAME at:2];
  118. /* initialize formatButton and its matrix of MenuCells with default value */
  119.     sprintf(param[5], "INPUT_CONVERT    %s *", PPMer);
  120.     [formatButton setTitle: "TIFF"];
  121.     [formatMatrix selectCellWithTag:TIFF];
  122.     [converterinfo setEnabled:NO];
  123.     [converterinfo setTitle:"pbmplus Directory:" at:0];
  124.     [converterinfo setStringValue:PBMPLUSDIRECTORY at:0];
  125.     return self;
  126. }
  127.  
  128. - setConverterInfo
  129. {
  130.     strcpy(param[4], "BASE_FILE_FORMAT    PPM");
  131.     [converterinfo setTitle:"pbmplus Directory:" at:0];
  132.     [converterinfo setStringValue:PBMPLUSDIRECTORY at:0];
  133.     [converterinfo setEnabled:YES];
  134.     return self;
  135. }
  136.  
  137.  
  138.  
  139. - doTIFF:sender
  140. {
  141.     strcpy(param[4], "BASE_FILE_FORMAT    PPM");
  142.     sprintf(param[5], "INPUT_CONVERT    %s *", PPMer);
  143.     [converterinfo setEnabled:NO];
  144.     return self;
  145. }
  146.  
  147. - doGIF:sender
  148. {
  149.     strcpy(converter, "giftoppm");
  150.     [self setConverterInfo];
  151.     return self;
  152. }
  153.  
  154. - doGOULD:sender
  155. {
  156.     strcpy(converter, "gouldtoppm");
  157.     [self setConverterInfo];
  158.     return self;
  159. }
  160.  
  161. - doILBM:sender
  162. {
  163.     strcpy(converter, "ilbmtoppm");
  164.     [self setConverterInfo];
  165.     return self;
  166. }
  167.  
  168. - doIMG:sender
  169. {
  170.     strcpy(converter, "imgtoppm");
  171.     [self setConverterInfo];
  172.     return self;
  173. }
  174.  
  175. - doMTV:sender
  176. {
  177.     strcpy(converter, "mtvtoppm");
  178.     [self setConverterInfo];
  179.     return self;
  180. }
  181.  
  182. - doPCX:sender
  183. {
  184.     strcpy(converter, "pcxtoppm");
  185.     [self setConverterInfo];
  186.     return self;
  187. }
  188.  
  189. - doPGM:sender
  190. {
  191.     strcpy(converter, "pgmtoppm");
  192.     [self setConverterInfo];
  193.     return self;
  194. }
  195.  
  196. - doPI1:sender
  197. {
  198.     strcpy(converter, "pi1toppm");
  199.     [self setConverterInfo];
  200.     return self;
  201. }
  202.  
  203. - doPICT:sender
  204. {
  205.     strcpy(converter, "picttoppm");
  206.     [self setConverterInfo];
  207.     return self;
  208. }
  209.  
  210. - doPJ:sender
  211. {
  212.     strcpy(converter, "pjtoppm");
  213.     [self setConverterInfo];
  214.     return self;
  215. }
  216.  
  217. - doQRT:sender
  218. {
  219.     strcpy(converter, "qrttoppm");
  220.     [self setConverterInfo];
  221.     return self;
  222. }
  223.  
  224. - doRAW:sender
  225. {
  226.     strcpy(converter, "rawtoppm");
  227.     strcpy(param[4], "BASE_FILE_FORMAT    PPM");
  228.     sprintf(param[5], "INPUT_CONVERT    %s/%s ", 
  229.         [converterinfo stringValueAt:0], converter);
  230.     [converterinfo setTitle:"WIDTHxHEIGHT:" at:0];
  231.     [converterinfo setStringValue:FRAMEWIDTHxHEIGHT at:0];
  232.     [converterinfo setEnabled:YES];
  233.     return self;
  234. }
  235.  
  236. - doRGB3:sender
  237. {
  238.     strcpy(converter, "rgb3toppm");
  239.     [self setConverterInfo];
  240.     return self;
  241. }
  242.  
  243. - doSLD:sender
  244. {
  245.     strcpy(converter, "sldtoppm");
  246.     [self setConverterInfo];
  247.     return self;
  248. }
  249.  
  250. - doSPC:sender
  251. {
  252.     strcpy(converter, "spctoppm");
  253.     [self setConverterInfo];
  254.     return self;
  255. }
  256.  
  257. - doSPU:sender
  258. {
  259.     strcpy(converter, "sputoppm");
  260.     [self setConverterInfo];
  261.     return self;
  262. }
  263.  
  264. - doTGA:sender
  265. {
  266.      strcpy(converter, "tgatoppm");
  267.     [self setConverterInfo];
  268.     return self;
  269. }
  270.  
  271. - doXIM:sender
  272. {
  273.     strcpy(converter, "ximtoppm");
  274.     [self setConverterInfo];
  275.     return self;
  276. }
  277.  
  278. - doXPM:sender
  279. {
  280.     strcpy(converter, "xpmtoppm");
  281.     [self setConverterInfo];
  282.     return self;
  283. }
  284.  
  285. - doYUV:sender
  286. {
  287.     strcpy(param[4], "BASE_FILE_FORMAT    YUV\nYUV_SIZE    ");
  288.      strcpy(param[5], "INPUT_CONVERT    *");
  289.     [converterinfo setTitle:"WIDTHxHEIGHT:" at:0];
  290.     [converterinfo setStringValue:FRAMEWIDTHxHEIGHT at:0];
  291.     [converterinfo setEnabled:YES];
  292.     return self;
  293. }
  294.  
  295. - doPPM:sender
  296. {
  297.     strcpy(param[4], "BASE_FILE_FORMAT    PPM");
  298.     strcpy(param[5], "INPUT_CONVERT    *");
  299.     [converterinfo setEnabled:NO];
  300.     return self;
  301. }
  302.  
  303. - doSPECIAL:sender
  304. {
  305.     strcpy(param[4], "BASE_FILE_FORMAT    PPM");
  306.     [converterinfo setTitle:"FrameType:" at:0];
  307.     [converterinfo setStringValue: SPECIALFRAMETYPE at:0];
  308.     [converterinfo setEnabled:YES];
  309.     return self;
  310. }
  311.  
  312. - toMPEGMethod:sender
  313. {
  314.     char *tmp, *animdirectory, *moviename, *moviepath, *frametype; 
  315.     char *fullpath, *framefile; 
  316.     char *paramfile, *resultfile, *execstring;
  317.     int startframe, endframe, numframes, i, width, height, exit_status;
  318.     FILE *pfile;
  319.  
  320.     animdirectory = (char *)malloc(100);
  321.     moviepath = (char *)malloc(100);
  322.     frametype = (char *)malloc(100);
  323.     fullpath = (char *)malloc(200);
  324.     framefile = (char *)malloc(300);
  325.     paramfile = (char *)malloc(100);
  326.     resultfile = (char *)malloc(100);
  327.     execstring = (char *)malloc(100);
  328.  
  329. /* store user's table entries into variables     */
  330.  
  331.     strcpy(animdirectory, [animinfo stringValueAt:0]);
  332.     startframe = [animinfo intValueAt:1];
  333.     endframe = [animinfo intValueAt:2];
  334.     
  335.     sprintf(param[6], "INPUT_DIR    %s", animdirectory);
  336.     numframes = endframe - startframe + 1;
  337.     sprintf(param[2], "GOP_SIZE    %d", numframes);
  338.  
  339.     moviename = stripname(animdirectory);
  340.     strcpy(moviepath, "/tmp/");
  341.     sprintf(param[1], "OUTPUT    %s%s.mpg", moviepath, moviename);
  342.  
  343.     strcpy(frametype, [formatButton title]);
  344.     tmp = frametype; do *tmp = tolower(*tmp); while(*tmp++);
  345.     switch([[formatMatrix selectedCell] tag]) {
  346.         case YUV:
  347.             sprintf(param[8], "%s.*.%s    [%d-%d]", 
  348.                 moviename, frametype, startframe, endframe);
  349.             strcat(param[4], [converterinfo stringValueAt:0]);
  350.             break;
  351.         case RGB3:
  352.             sprintf(param[8], "%s.*    [%d-%d]", 
  353.                 moviename, startframe, endframe);
  354.             sprintf(param[5], "INPUT_CONVERT    %s/%s *.r *.g *.b", 
  355.                 [converterinfo stringValueAt:0], converter);
  356.             break;
  357.         case RAW:
  358.             sprintf(param[8], "%s.*.%s    [%d-%d]", 
  359.                 moviename, frametype, startframe, endframe);
  360.             width = atoi((tmp = [converterinfo stringValueAt:0]));
  361.             height = atoi(strpbrk(tmp, "xX") + 1);
  362.             sprintf(param[5], "%s %d %d *", param[5], width, height);
  363.             break;
  364.         case SPECIAL:
  365.             strcpy(frametype, [converterinfo stringValueAt:0]);
  366.             sprintf(param[8], "%s.*.%s    [%d-%d]", 
  367.                 moviename, frametype, startframe, endframe);
  368.             strcpy(MyConverter, PPMer);
  369.             *(strrchr(MyConverter, '/') + 1) = '\0';
  370.             strcat(MyConverter, frametype);
  371.             sprintf(param[5], "INPUT_CONVERT    %s2ppm *", MyConverter);
  372.             break;
  373.         default: 
  374.             sprintf(param[8], "%s.*.%s    [%d-%d]", 
  375.                 moviename, frametype, startframe, endframe);
  376.             if([converterinfo isEnabled]) {
  377.                 sprintf(param[5], "INPUT_CONVERT    %s/%s *", 
  378.                     [converterinfo stringValueAt:0], converter);
  379.             }
  380.             break;
  381.     }
  382.  
  383.     
  384.     if(*animdirectory == '~') {
  385.         strcpy(fullpath, NXHomeDirectory());
  386.         strcat(fullpath, animdirectory + 1);
  387.     }
  388.     else strcpy(fullpath, animdirectory);
  389.  
  390. /* check that the FIRST frame file exists      */
  391.     sprintf(framefile, "%s/%s.%d.%s", 
  392.             fullpath, moviename, startframe, frametype);
  393.     if(access(framefile, R_OK)) {
  394.         NXRunAlertPanel("Cannot read file!", "First frame %s not accessible!",
  395.             "OK",NULL,NULL,
  396.             framefile);
  397.         return self;
  398.     }
  399. /* check that the LAST frame file exists      */
  400.     sprintf(framefile, "%s/%s.%d.%s", 
  401.             fullpath, moviename, endframe, frametype);
  402.     if(access(framefile, R_OK)) {
  403.         NXRunAlertPanel("Cannot read file!", "Last frame %s not accessible!",
  404.             "OK",NULL,NULL,
  405.             framefile);
  406.         return self;
  407.     }
  408.  
  409.  
  410. /* create the parameter file on /tmp           */
  411.     strcpy(paramfile, "/tmp/");
  412.     strcat(strcat(paramfile, moviename), ".param");
  413.     pfile = fopen(paramfile, "w");
  414.     for(i=0; i<19; i++) fprintf(pfile, "%s\n", param[i]);
  415.     fclose(pfile);
  416.  
  417. /* execute "mpeg-encode paramfile > resultfile"           */
  418.     strcpy(resultfile, "/tmp/");
  419.     strcat(strcat(resultfile, moviename), ".result");
  420.     sprintf(execstring, "%s %s > %s &", MPEGer, paramfile, resultfile);
  421.     if(!(exit_status = system(execstring)))
  422.         NXRunAlertPanel("Submitted MPEG Job to sh",
  423. "Parameters: %s\nStatistics: %s\nMPEG: %s%s.mpg\nAny errors are listed in the console.",
  424.         "OK",NULL,NULL,
  425.          paramfile, resultfile, moviepath, moviename);
  426.     else     NXRunAlertPanel("sh reports problem with MPEG Job!",
  427.         "Errors are listed in the console.",
  428.         "OK",NULL,NULL);
  429.  
  430.     return self;
  431. }
  432.  
  433.  
  434. @end
  435.